/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "datatypedefinitions.h"
#include "gimiprotocoltypes.h"

namespace gimi {

DataTypeDefinition::DataTypeDefinition()
  : dataTypeId (-1),
    dataTypeName (),
    dataTypeDescription ()
{

}
  
DataTypeDefinition::DataTypeDefinition(const int typeId,
                                       const std::string &typeName,
                                       const std::string &typeDescription)
  : dataTypeId (typeId),
    dataTypeName (typeName),
    dataTypeDescription (typeDescription)
{

}

DataTypeDefinition::~DataTypeDefinition()
{

}

DataTypeDefinitions::~DataTypeDefinitions()
{
  ownMutex_Destroy(definitionMutex);
}

bool DataTypeDefinitions::addDefinition(const int typeId,
                                        const std::string &typeName,
                                        const std::string &typeDescription)
{
  bool result = false;
  DataTypeDefinition definition(typeId, typeName, typeDescription);
  
  ownMutex_Lock(definitionMutex);
  if ((typeMap.insert(t_datatypeMapPair(typeId, definition))).second) {
    // Description was successfully added
    result = true;
  } else { // Typeid already existed
    result = false;
    dPrint(1, "Warning, could not add datatype %d (%s, %s) because it already "
        "existed", typeId, typeName.c_str(), typeDescription.c_str());
  }
  ownMutex_Unlock(definitionMutex);
  return result;
}

bool DataTypeDefinitions::getDatatypeName(std::string& typeName,
                                          const int typeId)
{
  bool result = false;

  ownMutex_Lock(definitionMutex);
  t_datatypeMapConstIterator iter = typeMap.find(typeId);
  if (iter != typeMap.end()) { // Found typeid.
    typeName = iter->second.dataTypeName;
    result = true;
  } else { // Typeid not found.
    typeName = "";
    result = false;
  }
  ownMutex_Unlock(definitionMutex);
  return result;
}

bool DataTypeDefinitions::getDatatypeDescription(std::string& typeDescription,
                                                 const int typeId)
{
  bool result = false;
  
  ownMutex_Lock(definitionMutex);
  t_datatypeMapConstIterator iter = typeMap.find(typeId);
  if (iter != typeMap.end()) { // Found typeid.
    typeDescription = iter->second.dataTypeDescription;
    result = true;
  } else { // Typeid not found.
    typeDescription = "";
    result = false;
  }

  ownMutex_Unlock(definitionMutex);
  return result;
}

bool DataTypeDefinitions::getDatatype(DataTypeDefinition& definition,
                                      const int typeId)
{
  bool result = false;
  
  ownMutex_Lock(definitionMutex);
  t_datatypeMapConstIterator iter = typeMap.find(typeId);
  if (iter != typeMap.end()) { // Found typeid.
    definition = iter->second;
    result = true;
  } else { // Typeid not found.
    DataTypeDefinition emptyDef;
    definition = emptyDef;
    result = false;
  }
  ownMutex_Unlock(definitionMutex);
  return result;
}

} // end namespace gimi
